using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using Crosland.Framework.DataHelper;
using Crosland.Framework.Base;
using Crosland.Offline.Core;
using Crosland.Offline.Data.Helper;

namespace Crosland.Offline.Data.Dao
{
    public class ParametroDao : DaoBase
    {
        public ParametroDao(string strConn) : base(strConn)
        {
        }

        ParametroHelper helper = new ParametroHelper();

        public List<Parametro> GetByCriteria()
        {
            List<Parametro> entitys = new List<Parametro>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);
            
            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public void Update(Parametro entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpUpdate);

            dbProvider.AddInParameter(command, helper.IdTabla, DbType.String, entity.IdTabla);
            dbProvider.AddInParameter(command, helper.DesValor, DbType.String, entity.DesValor);
            dbProvider.AddInParameter(command, helper.Id, DbType.Int32, entity.ID);

            dbProvider.ExecuteNonQuery(command);
        }

        public List<Parametro> List()
        {
            List<Parametro> entitys = new List<Parametro>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpList);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    Parametro entity = new Parametro();

                    int iIdTabla = dr.GetOrdinal(helper.IdTabla);
                    if(!dr.IsDBNull(iIdTabla))entity.IdTabla = dr.GetString(iIdTabla);

                    entity.DesParametro = entity.IdTabla.Replace('.', ' ');
                    entity.DesParametro = entity.DesParametro.ToUpper();

                    entitys.Add(entity);
                }
            }

            return entitys;
        }


        public List<Parametro> GetByCriteria(string idTabla)
        {
            List<Parametro> entitys = new List<Parametro>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);
            dbProvider.AddInParameter(command, helper.IdTabla, DbType.String, idTabla);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }
    }
}
